package com.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000116670759";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCm7Q054DlNCk1zO12qJIrIGAtpM2hK5lzT+2Ay0A6xK3FAXx3qZI//ZwSoZcDutOCe9N8YpUxszI0DeBcztzZrhksGYtl0T2kjuqjWK6dDRpke7N9JBnixcv5bUuZaSh6CaSXHBb+IwLxHEe+Yq5V5Z709QYY7d7RFCuozvQ0zTUYu3b8PBNCDL9FUF4xiqt+47H3aWiMFhjT9VoI7UdhQ4NgJPb7X4M8pvsCKlGi/quqAORkz+NPkZhLkXQW9pqDlcXwsXKBQ38A2V44MQUhcVN5RRYeQYPeluG1NAaWnvtQvTrtA4xw5e8LKZq4DCxqzrzUp8e6HBtVNK/xZM465AgMBAAECggEAO3vZ7Lfkc3mqY9fR5uGiuu8mkdSJ47b3eWrouvK9aZEriF7c0DwJvF+/nyqzd9zo9nwYTDYLZ/H0hjnzOrCdKe/vKFVtXC84Lk1EAR4JOsYbppOOdf1iYQmzbmO6Ro6HFL3Xgnaf7GGSNllD4kmyuFKpcvy4ktRzXu8myk0iORdKrE92PzMolgH6iJ/RSPkwm8jRKaiUAklhpfrhSlSNokssqV+WpGCNCA8eY4iX677uXwIKe1WC7Q/v1TMIlERCsAjRz6I5G/s3IP8qxWPH2fMeHQx+mcVBbhOBDrwkc/EUqbpBIQPvVJqzyaYEbfpMnMNxV5hApSOJmLt+6NMQAQKBgQDdsjhYyBL7si4sC4nsN/vUXxn24RgOvMPfsdNh5ynJF6mlG7E7UqkeavWdARStdv3Rnnz1Ms2nMeWhsd0rSKdLyyfwO4iGedQr5usxTWE9qDprq0BiyDqnpas6rpRf0VN+sDJuxQcD4zWEFcZELaUMYcz+Gd66OZRLjV3jYw7zuQKBgQDAwUlyTkunbBCRzwy3gau3cG6GKEPK/BPd/p70gy8Y54UgVLIn9+4IvT/ZbcFJ5GOWLMY7JAFMSFxj5jJEyxp8MF6d5BzQFltH4gAKhs+V65pTmmTdgX3zAh/BI/imJXVYqqPznTzLqjJr/tyMbcfOW4eERAm7EpS7B7lRaSzzAQKBgQCH0kMxKUpwB4bAF0Wlgdr4Dlei597MUJuUNabyiSQRfjzB2choNslDyHW3fc0ERS1VukqGElzqPOiZepWsifNsvQc359FJyePSDcQzzVtw4dsA0cc9fbxpr1bLOd/PIDiOGjabpHwaxp2n0YtJg8llX5muZ30MDHCtJOvLfonPiQKBgDEhEtdPnXjO0mpXm6YqJtpajdEj8iOKnIW4oEsyJI70c7oJ3F7TkpgnpF6G2v06eSeNwfjQ9IO87c+InMdr/RQqP/N+cNmY2Ss5L0l/kuljY7JrZ8oNd0y4/d78jCPM6FvxIli1ewluF+vuycgBMywlqCTOMqy7z6zcCx/07sABAoGAePoyf3BEWsOZBhQTyK3OD1P+8BusrYZqEU07VJB/TXLiOyWYpv86j5II4AC3bvXQFFvXDKkg5O+DVjy8ge6S6Ilr36dUosiTSdJRp7mmwLzvAC1EyGCBZ5Ol2Bspo9ITQ9gONt32Rt2I7NGcJ0XujY2Iogd5/Lj+6CXqKnr/U8Y=";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsLWLF1BLlTsmSjLe/PGhl11QBXk4CYK7AtzRFL6RV+h74CJGTIOa+wxUMocaW/1HEBmiDCq4SJrRXWYo/CQ849qGfzyQsPIKiBmbnStGF52GEqKLQOK2W55Kxc8CjX6owhuR7J3c6fY0qZVO3/uSv4rrzoId0P7jttLGYNzqGuAHFgj6UiZE/hX2i30N1Yqouu1JSBrk53F7BnnU3IWfpIGS9mpcV9A63BZlBcpb3KSReX6nt7mh2xPV0GDeOpkCOZBBAGPoo0ZBNhgMSaUjtYiFWSN0rBoCzZnQctDWI1z2Q9//CdrcqL1tnWNkUrYauLqPWhFh4QldLpkbv/+WtQIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private String notify_url = "http://69iz40ayev.54http.tech/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    private String timeout = "30m";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
